#include "CSplitter.h"
#include <QHBoxLayout>
#include <QDebug>

CSplitter::CSplitter(QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->setContentsMargins(0,0,0,0);
}

/* @dst:将要分割的已存在的控件
 * @src:新加的控件
 * @orientation:分割方向
*/
void CSplitter::addWidget(QWidget *dst, QWidget *src, Qt::Orientation orientation)
{
    if(!src)
        return ;

    if ((nullptr == dst) || (0 == this->layout()->count()))
    {
        QSplitter *pSplitter = new QSplitter(this);
        pSplitter->addWidget(src);
        this->layout()->addWidget(pSplitter);
    }
    else
    {
        QSplitter *pPSplitter = qobject_cast<QSplitter *>(dst->parentWidget());

        if (!pPSplitter)
            return ;

        pPSplitter->setOrientation(orientation);

        QSplitter *pSplitterDst = new QSplitter(this);
        pSplitterDst->addWidget(dst);

        QSplitter *pSplitterSrc = new QSplitter(this);
        pSplitterSrc->addWidget(src);

        pPSplitter->addWidget(pSplitterDst);
        pPSplitter->addWidget(pSplitterSrc);
    }
}

void CSplitter::removeWidget(QWidget *src)
{
    if(!src || !src->parentWidget())
        return ;

    QSplitter *pPPSplitter = qobject_cast<QSplitter *>(src->parentWidget()->parentWidget());
    if (nullptr == pPPSplitter)
    {
        if(qobject_cast<QSplitter *>(src->parentWidget()))
            delete src->parentWidget(); //src & splitter
    }
    else
    {
        QSplitter *pUSplitter = (QSplitter *)pPPSplitter->widget(!pPPSplitter->indexOf(src->parentWidget())); //QSplitter只会装两个 index非0即1

        QSplitter *pPPPSplitter = qobject_cast<QSplitter *>(pPPSplitter->parentWidget());
        if (nullptr == pPPPSplitter) {
            this->layout()->addWidget(pUSplitter);
        } else {
            pPPPSplitter->insertWidget(pPPPSplitter->indexOf(pPPSplitter),pUSplitter);
        }

        delete pPPSplitter; //删除src 删除pSplitter
    }

    qDebug()<<"开始打印剩余控件------------";
    recursiveInstallEventFilter(this);
    qDebug()<<"打印完成------------";
}

void CSplitter::addWidget2(QWidget *dst, QWidget *src, Qt::Orientation orientation)
{
    if(!src)
        return ;

    if ((nullptr == dst) || (0 == this->layout()->count()))
    {
        QSplitter *pSplitter = new QSplitter(orientation,this);
        pSplitter->addWidget(src);
        this->layout()->addWidget(pSplitter);
    }
    else
    {
        QSplitter *pPSplitter = qobject_cast<QSplitter *>(dst->parentWidget());
        if(pPSplitter->count() > 1){
            QSplitter *pSplitter = new QSplitter(orientation,this);
            pPSplitter->insertWidget(pPSplitter->indexOf(dst),pSplitter);

            pSplitter->addWidget(dst);
            pSplitter->addWidget(src);
        }else{
            pPSplitter->setOrientation(orientation);
            pPSplitter->addWidget(src);
        }
    }
}

void CSplitter::removeWidget2(QWidget *src)
{
    if(!src || !src->parentWidget())
        return ;

    QSplitter *pPSplitter = qobject_cast<QSplitter *>(src->parentWidget());
    if(pPSplitter->count() > 1){
        QWidget *pBWidget = pPSplitter->widget(!pPSplitter->indexOf(src));

        QSplitter *pPPSplitter = qobject_cast<QSplitter *>(pPSplitter->parentWidget());
        if(nullptr == pPPSplitter){
            if(nullptr == qobject_cast<QSplitter *>(pBWidget))
            {
                delete src;
                return ;
            }else{
                this->layout()->addWidget(pBWidget);
            }
        }else{
            pPPSplitter->insertWidget(pPPSplitter->indexOf(pPSplitter),pBWidget);
        }
    }

    delete pPSplitter;

    qDebug()<<"开始打印剩余控件------------";
    recursiveInstallEventFilter(this);
    qDebug()<<"打印完成------------";
}

void CSplitter::addWidget3(QWidget *dst, QWidget *src, Qt::Orientation orientation)
{
    if(!src)
        return ;

    if ((nullptr == dst) || (0 == this->layout()->count()))
    {
        QSplitter *pSplitter = new QSplitter(orientation,this);
        pSplitter->addWidget(src);
        this->layout()->addWidget(pSplitter);
    }
    else
    {
        QSplitter *pPSplitter = qobject_cast<QSplitter *>(dst->parentWidget());
        if(orientation == pPSplitter->orientation()){
            qDebug()<<"+++++"<<dst->size()<<dst->baseSize()<<dst->sizePolicy()<<dst->sizeHint()<<dst->minimumSize()<<dst->maximumSize();
            qDebug()<<"+++++"<<src->size()<<src->baseSize()<<src->sizePolicy()<<src->sizeHint()<<src->minimumSize()<<src->maximumSize();
            pPSplitter->insertWidget(pPSplitter->indexOf(dst) + 1,src);
            for(int i = 0; i < pPSplitter->count(); i++){
                pPSplitter->setStretchFactor(i,1);
            }
        }else{
            QSplitter *pSplitter = new QSplitter(orientation,this);
//            pSplitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
            pPSplitter->insertWidget(pPSplitter->indexOf(dst),pSplitter);

            qDebug()<<"-----"<<dst->size()<<dst->baseSize()<<dst->sizePolicy()<<dst->sizeHint()<<dst->minimumSize()<<dst->maximumSize();
            qDebug()<<"-----"<<src->size()<<src->baseSize()<<src->sizePolicy()<<src->sizeHint()<<src->minimumSize()<<src->maximumSize();

//            dst->resize(0,0);
//            src->resize(0,0);
            pSplitter->addWidget(dst);
            pSplitter->addWidget(src);
            qDebug()<<"-----"<<dst->size()<<dst->baseSize()<<dst->sizePolicy()<<dst->sizeHint()<<dst->minimumSize()<<dst->maximumSize();
            qDebug()<<"-----"<<src->size()<<src->baseSize()<<src->sizePolicy()<<src->sizeHint()<<src->minimumSize()<<src->maximumSize();


//            pSplitter->setStretchFactor(0,1);
//            pSplitter->setStretchFactor(1,1);
        }
    }


}

void CSplitter::removeWidget3(QWidget *src,bool clearEmptySplitter)
{
    if(!src || !src->parentWidget())
        return ;


    //清理空的Splitter
    if(clearEmptySplitter)
    {
        QSplitter *pSrcSplitter = qobject_cast<QSplitter *>(src);
        if(pSrcSplitter){
            QWidget *parent = pSrcSplitter->parentWidget();
            if(0 == pSrcSplitter->count())
                delete pSrcSplitter;

            removeWidget3(parent,true);
        }
    }
    else
    {
        QSplitter *pPSplitter = qobject_cast<QSplitter *>(src->parentWidget());
        delete src;
        if(pPSplitter)
            removeWidget3(pPSplitter,true);//递归查找空的父Splitter 并删除
    }

    qDebug()<<"开始打印剩余控件------------";
    recursiveInstallEventFilter(this);
    qDebug()<<"打印完成------------";
}

void CSplitter::recursiveInstallEventFilter(QObject *pObject)
{
    if(pObject->children().count() > 0) {
        foreach (QObject *objChild, pObject->children()) {
            qDebug()<<"子控件"<<objChild;
            recursiveInstallEventFilter(objChild);
        }
    }
}
